//
//  BANewFeatureCell.swift
//  BAweibo
//
//  Created by baggio on 2017/8/15.
//  Copyright © 2017年 baggio. All rights reserved.
//

import UIKit

class BANewFeatureCell: UICollectionViewCell {
    
    //提供一个属性供外界赋值
    var imageIndex:Int=0{
        didSet{
            imageView.image=UIImage(named: "new_feature_\((imageIndex)+1)")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI(){
        addSubview(imageView)
        
        imageView.snp.makeConstraints { (m) in
            m.size.equalTo(self)
            //            m.center.equalTo(self)
        }
        //        //这句话等价
        //        imageView.frame=bounds
        
        addSubview(startBtn)
        startBtn.snp.makeConstraints { (m) in
            m.centerX.equalTo(self)
            m.bottom.equalTo(self).multipliedBy(0.7)
        }
        startBtn.isHidden=true//前3个 cell 的时候隐藏
        
    }
    
    //最后一个 cell 才动画显示
    //有个 bug，按钮显示后，再往左划，第1个页面显示按钮，第2第3个页面不显示
    func showStartBtnAnimation(){
        startBtn.isHidden=false
        startBtn.transform=CGAffineTransform.init(scaleX: 0, y: 0)
        //禁止用户交互，动画结束才允许
        startBtn.isUserInteractionEnabled=false
        
        UIView.animate(withDuration: 1.6, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 8, options: [], animations: {
            //恢复原先状态
            self.startBtn.transform=CGAffineTransform.identity
        }) { (_) in
            //动画完成以后
            self.startBtn.isUserInteractionEnabled=true
        }
    }
    
    private lazy var imageView:UIImageView=UIImageView()
    
    //最后一个 cell 要显示一个按钮
    private lazy var startBtn:UIButton=UIButton(title: "开始体验", titleColor: UIColor.white, titleColorHighlight: nil, titleLabelFont: nil, imgName: nil, backgroundImgName: "new_feature_finish_button", target: self, action: #selector(btnClick))
    
    @objc private func btnClick(){
        print("开始体验")
        
        //发送切换根控制器的通知 -> BAMainViewController
        NotificationCenter.default.post(name: BA_NOTIFICATION_SWITCH_ROOT_VC, object: "fromNewFeatureVC2MainVC")
    }
}
